<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Introduction - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.filter.introduction.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.filter.introduction.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.filter.html">Zend_Filter</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.filter.html">Zend_Filter</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.filter.set.html">Standard Filter Classes</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.filter.introduction" class="section"><div class="info"><h1 class="title">Introduction</h1></div>
    

    <p class="para">
        The <span class="classname">Zend_Filter</span> component provides a set of commonly needed data
        filters. It also provides a simple filter chaining mechanism by which
        multiple filters may be applied to a single datum in a user-defined
        order.
    </p>

    <div class="section" id="zend.filter.introduction.definition"><div class="info"><h1 class="title">What is a filter?</h1></div>
        

        <p class="para">
            In the physical world, a filter is typically used for removing
            unwanted portions of input, and the desired portion of the input
            passes through as filter output (e.g., coffee). In such scenarios,
            a filter is an operator that produces a subset of the input. This
            type of filtering is useful for web applications - removing illegal
            input, trimming unnecessary white space, etc.
        </p>

        <p class="para">
            This basic definition of a filter may be extended to include
            generalized transformations upon input. A common transformation
            applied in web applications is the escaping of <acronym class="acronym">HTML</acronym> entities. For
            example, if a form field is automatically populated with untrusted
            input (e.g., from a web browser), this value should either be free
            of <acronym class="acronym">HTML</acronym> entities or contain only escaped <acronym class="acronym">HTML</acronym>
            entities, in order to prevent undesired behavior and security vulnerabilities. To meet
            this requirement, <acronym class="acronym">HTML</acronym> entities that appear in the input must
            either be removed or escaped. Of course, which approach is more
            appropriate depends on the situation. A filter that removes the
            <acronym class="acronym">HTML</acronym> entities operates within the scope of the first definition of
            filter - an operator that produces a subset of the input. A filter
            that escapes the <acronym class="acronym">HTML</acronym> entities, however, transforms the input
            (e.g., &quot;&amp;&quot; is transformed to
            &quot;&amp;amp;&quot;). Supporting such use cases for web
            developers is important, and &quot;to filter,&quot; in the context of using
            <span class="classname">Zend_Filter</span>, means to perform some transformations upon input
            data.
        </p>
    </div>

    <div class="section" id="zend.filter.introduction.using"><div class="info"><h1 class="title">Basic usage of filters</h1></div>
        

        <p class="para">
            Having this filter definition established provides the foundation
            for <span class="classname">Zend_Filter_Interface</span>, which requires a single
            method named  <span class="methodname">filter()</span> to be implemented by a filter
            class.
        </p>

        <p class="para">
            Following is a basic example of using a filter upon two input data,
            the ampersand (<em class="emphasis">&amp;</em>) and double quote
            (<em class="emphasis">&quot;</em>) characters:
        </p>

        <pre class="programlisting brush: php">
$htmlEntities = new Zend_Filter_HtmlEntities();

echo $htmlEntities-&gt;filter(&#039;&amp;&#039;); // &amp;amp;
echo $htmlEntities-&gt;filter(&#039;&quot;&#039;); // &amp;quot;
</pre>

    </div>

    <div class="section" id="zend.filter.introduction.static"><div class="info"><h1 class="title">Using the static staticFilter() method</h1></div>
        

        <p class="para">
            If it is inconvenient to load a given filter class and create an
            instance of the filter, you can use the static method
             <span class="methodname">Zend_Filter::filterStatic()</span> as an alternative invocation style.
            The first argument of this method is a data input value, that you
            would pass to the  <span class="methodname">filter()</span> method. The second
            argument is a string, which corresponds to the basename of the
            filter class, relative to the <span class="classname">Zend_Filter</span> namespace. The
             <span class="methodname">staticFilter()</span> method automatically loads the class, creates
            an instance, and applies the  <span class="methodname">filter()</span> method to the data
            input.
        </p>

        <pre class="programlisting brush: php">
echo Zend_Filter::filterStatic(&#039;&amp;&#039;, &#039;HtmlEntities&#039;);
</pre>


        <p class="para">
            You can also pass an array of constructor arguments, if they
            are needed for the filter class.
        </p>

        <pre class="programlisting brush: php">
echo Zend_Filter::filterStatic(&#039;&quot;&#039;,
                               &#039;HtmlEntities&#039;,
                               array(&#039;quotestyle&#039; =&gt; ENT_QUOTES));
</pre>


        <p class="para">
            The static usage can be convenient for invoking a filter ad hoc,
            but if you have the need to run a filter for multiple inputs,
            it&#039;s more efficient to follow the first example above,
            creating an instance of the filter object and calling its
             <span class="methodname">filter()</span> method.
        </p>

        <p class="para">
            Also, the <span class="classname">Zend_Filter_Input</span> class allows you to instantiate and
            run multiple filter and validator classes on demand to process
            sets of input data. See <a href="zend.filter.input.html" class="link">Zend_Filter_Input</a>.
        </p>

        <div class="section" id="zend.filter.introduction.static.namespaces"><div class="info"><h1 class="title">Namespaces</h1></div>
            

            <p class="para">
                When working with self defined filters you can give a fourth parameter
                to  <span class="methodname">Zend_Filter::filterStatic()</span> which is the namespace
                where your filter can be found.
            </p>

            <pre class="programlisting brush: php">
echo Zend_Filter::filterStatic(
    &#039;&quot;&#039;,
    &#039;MyFilter&#039;,
    array($parameters),
    array(&#039;FirstNamespace&#039;, &#039;SecondNamespace&#039;)
);
</pre>


            <p class="para">
                <span class="classname">Zend_Filter</span> allows also to set namespaces as default.
                This means that you can set them once in your bootstrap and have not to give
                them again for each call of  <span class="methodname">Zend_Filter::filterStatic()</span>.
                The following code snippet is identical to the above one.
            </p>

            <pre class="programlisting brush: php">
Zend_Filter::setDefaultNamespaces(array(&#039;FirstNamespace&#039;, &#039;SecondNamespace&#039;));
echo Zend_Filter::filterStatic(&#039;&quot;&#039;, &#039;MyFilter&#039;, array($parameters));
echo Zend_Filter::filterStatic(&#039;&quot;&#039;, &#039;OtherFilter&#039;, array($parameters));
</pre>


            <p class="para">
                For your convenience there are following methods which allow the handling of
                namespaces:
            </p>

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <em class="emphasis"> <span class="methodname">Zend_Filter::getDefaultNamespaces()</span></em>:
                        Returns all set default namespaces as array.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis"> <span class="methodname">Zend_Filter::setDefaultNamespaces()</span></em>:
                        Sets new default namespaces and overrides any previous set. It accepts
                        either a string for a single namespace of an array for multiple namespaces.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis"> <span class="methodname">Zend_Filter::addDefaultNamespaces()</span></em>:
                        Adds additional namespaces to already set ones. It accepts either a string
                        for a single namespace of an array for multiple namespaces.
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <em class="emphasis"> <span class="methodname">Zend_Filter::hasDefaultNamespaces()</span></em>:
                        Returns <b><tt>TRUE</tt></b> when one or more default namespaces are
                        set, and <b><tt>FALSE</tt></b> when no default namespaces are set.
                    </p>
                </li>
            </ul>
        </div>
    </div>

    <div class="section" id="zend.filter.introduction.caveats"><div class="info"><h1 class="title">Double filtering</h1></div>
        

        <p class="para">
            When using two filters after each other you have to keep in mind that it is often not
            possible to get the original output by using the opposite filter. Take the following
            example:
        </p>

        <pre class="programlisting brush: php">
$original = &quot;my_original_content&quot;;

// Attach a filter
$filter   = new Zend_Filter_Word_UnderscoreToCamelCase();
$filtered = $filter-&gt;filter($original);

// Use it&#039;s opposite
$filter2  = new Zend_Filter_Word_CamelCaseToUnderscore();
$filtered = $filter2-&gt;filter($filtered)
</pre>


        <p class="para">
            The above code example could lead to the impression that you will get the original
            output after the second filter has been applied. But thinking logically this is not the
            case. After applying the first filter <em class="emphasis">my_original_content</em> will be
            changed to <em class="emphasis">MyOriginalContent</em>. But after applying the second filter
            the result is <em class="emphasis">My_Original_Content</em>.
        </p>

        <p class="para">
            As you can see it is not always possible to get the original output by using a filter
            which seems to be the opposite. It depends on the filter and also on the given input.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.filter.html">Zend_Filter</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.filter.html">Zend_Filter</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.filter.set.html">Standard Filter Classes</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.filter.html">Zend_Filter</a></li>
  <li class="active"><a href="zend.filter.introduction.html">Introduction</a></li>
  <li><a href="zend.filter.set.html">Standard Filter Classes</a></li>
  <li><a href="zend.filter.filter_chains.html">Filter Chains</a></li>
  <li><a href="zend.filter.writing_filters.html">Writing Filters</a></li>
  <li><a href="zend.filter.input.html">Zend_Filter_Input</a></li>
  <li><a href="zend.filter.inflector.html">Zend_Filter_Inflector</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>